home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / uemlsrc.arc / region.c < prev    next >
C/C++ Source or Header  |  1987-08-24  |  7KB  |  204 lines

  1. /*
  2.  * The routines in this file
  3.  * deal with the region, that magic space
  4.  * between "." and mark. Some functions are
  5.  * commands. Some functions are just for
  6.  * internal use.
  7.  */
  8. #include        <stdio.h>
  9. #include        "ed.h"
  10.  
  11. /*
  12.  * Kill the region. Ask "getregion"
  13.  * to figure out the bounds of the region.
  14.  * Move "." to the start, and kill the characters.
  15.  * Bound to "C-W".
  16.  */
  17. killregion(f, n)
  18. register int f, n;
  19. {
  20.         register int    s;
  21.         REGION          region;
  22.  
  23.         if ((s=getregion(®ion)) != TRUE)
  24.                 return (s);
  25.         if ((lastflag&CFKILL) == 0)             /* This is a kill type  */
  26.                 kdelete();                      /* command, so do magic */
  27.         thisflag |= CFKILL;                     /* kill buffer stuff.   */
  28.         curwp->w_dotp = region.r_linep;
  29.         curwp->w_doto = region.r_offset;
  30.         return (ldelete(region.r_size, TRUE));
  31. }
  32.  
  33. /*
  34.  * Copy all of the characters in the
  35.  * region to the kill buffer. Don't move dot
  36.  * at all. This is a bit like a kill region followed
  37.  * by a yank. Bound to "M-W".
  38.  */
  39. copyregion(f, n)
  40. register int f, n;
  41. {
  42.         register LINE   *linep;
  43.         register int    loffs;
  44.         register int    s;
  45.         REGION          region;
  46.  
  47.         if ((s=getregion(®ion)) != TRUE)
  48.                 return (s);
  49.         if ((lastflag&CFKILL) == 0)             /* Kill type command.   */
  50.                 kdelete();
  51.         thisflag |= CFKILL;
  52.         linep = region.r_linep;                 /* Current line.        */
  53.         loffs = region.r_offset;                /* Current offset.      */
  54.         while (region.r_size--) {
  55.                 if (loffs == llength(linep)) {  /* End of line.         */
  56.                         if ((s=kinsert('\n')) != TRUE)
  57.                                 return (s);
  58.                         linep = lforw(linep);
  59.                         loffs = 0;
  60.                 } else {                        /* Middle of line.      */
  61.                         if ((s=kinsert(lgetc(linep, loffs))) != TRUE)
  62.                                 return (s);
  63.                         ++loffs;
  64.                 }
  65.         }
  66.         return (TRUE);
  67. }
  68.  
  69. /*
  70.  * Lower case region. Zap all of the upper
  71.  * case characters in the region to lower case. Use
  72.  * the region code to set the limits. Scan the buffer,
  73.  * doing the changes. Call "lchange" to ensure that
  74.  * redisplay is done in all buffers. Bound to
  75.  * "C-X C-L".
  76.  */
  77. lowerregion(f, n)
  78. register int f, n;
  79. {
  80.         register LINE   *linep;
  81.         register int    loffs;
  82.         register int    c;
  83.         register int    s;
  84.         REGION          region;
  85.  
  86.         if ((s=getregion(®ion)) != TRUE)
  87.                 return (s);
  88.         lchange(WFHARD);
  89.         linep = region.r_linep;
  90.         loffs = region.r_offset;
  91.         while (region.r_size--) {
  92.                 if (loffs == llength(linep)) {
  93.                         linep = lforw(linep);
  94.                         loffs = 0;
  95.                 } else {
  96.                         c = lgetc(linep, loffs);
  97.                         if (c>='A' && c<='Z')
  98.                                 lputc(linep, loffs, c+'a'-'A');
  99.                         ++loffs;
  100.                 }
  101.         }
  102.         return (TRUE);
  103. }
  104.  
  105. /*
  106.  * Upper case region. Zap all of the lower
  107.  * case characters in the region to upper case. Use
  108.  * the region code to set the limits. Scan the buffer,
  109.  * doing the changes. Call "lchange" to ensure that
  110.  * redisplay is done in all buffers. Bound to
  111.  * "C-X C-L".
  112.  */
  113. upperregion(f, n)
  114. register int f, n;
  115. {
  116.         register LINE   *linep;
  117.         register int    loffs;
  118.         register int    c;
  119.         register int    s;
  120.         REGION          region;
  121.  
  122.         if ((s=getregion(®ion)) != TRUE)
  123.                 return (s);
  124.         lchange(WFHARD);
  125.         linep = region.r_linep;
  126.         loffs = region.r_offset;
  127.         while (region.r_size--) {
  128.                 if (loffs == llength(linep)) {
  129.                         linep = lforw(linep);
  130.                         loffs = 0;
  131.                 } else {
  132.                         c = lgetc(linep, loffs);
  133.                         if (c>='a' && c<='z')
  134.                                 lputc(linep, loffs, c-'a'+'A');
  135.                         ++loffs;
  136.                 }
  137.         }
  138.         return (TRUE);
  139. }
  140.  
  141. /*
  142.  * This routine figures out the
  143.  * bounds of the region in the current window, and
  144.  * fills in the fields of the "REGION" structure pointed
  145.  * to by "rp". Because the dot and mark are usually very
  146.  * close together, we scan outward from dot looking for
  147.  * mark. This should save time. Return a standard code.
  148.  * Callers of this routine should be prepared to get
  149.  * an "ABORT" status; we might make this have the
  150.  * conform thing later.
  151.  */
  152. getregion(rp)
  153. register REGION *rp;
  154. {
  155.         register LINE   *flp;
  156.         register LINE   *blp;
  157.         register int    fsize;
  158.         register int    bsize;
  159.  
  160.         if (curwp->w_markp == NULL) {
  161.                 mlwrite("No mark set in this window");
  162.                 return (FALSE);
  163.         }
  164.         if (curwp->w_dotp == curwp->w_markp) {
  165.                 rp->r_linep = curwp->w_dotp;
  166.                 if (curwp->w_doto < curwp->w_marko) {
  167.                         rp->r_offset = curwp->w_doto;
  168.                         rp->r_size = curwp->w_marko-curwp->w_doto;
  169.                 } else {
  170.                         rp->r_offset = curwp->w_marko;
  171.                         rp->r_size = curwp->w_doto-curwp->w_marko;
  172.                 }
  173.                 return (TRUE);
  174.         }
  175.         blp = curwp->w_dotp;
  176.         bsize = curwp->w_doto;
  177.         flp = curwp->w_dotp;
  178.         fsize = llength(flp)-curwp->w_doto+1;
  179.         while (flp!=curbp->b_linep || lback(blp)!=curbp->b_linep) {
  180.                 if (flp != curbp->b_linep) {
  181.                         flp = lforw(flp);
  182.                         if (flp == curwp->w_markp) {
  183.                                 rp->r_linep = curwp->w_dotp;
  184.                                 rp->r_offset = curwp->w_doto;
  185.                                 rp->r_size = fsize+curwp->w_marko;
  186.                                 return (TRUE);
  187.                         }
  188.                         fsize += llength(flp)+1;
  189.                 }
  190.                 if (lback(blp) != curbp->b_linep) {
  191.                         blp = lback(blp);
  192.                         bsize += llength(blp)+1;
  193.                         if (blp == curwp->w_markp) {
  194.                                 rp->r_linep = blp;
  195.                                 rp->r_offset = curwp->w_marko;
  196.                                 rp->r_size = bsize - curwp->w_marko;
  197.                                 return (TRUE);
  198.                         }
  199.                 }
  200.         }
  201.         mlwrite("Bug: lost mark");
  202.         return (FALSE);
  203. }
  204.